//
//  DEMOSecondViewController.m
//  RESideMenuExample
//
//  Created by Roman Efimov on 10/10/13.
//  Copyright (c) 2013 Roman Efimov. All rights reserved.
//

#import "HQBuyListViewController.h"
#import "HQZhou.h"
#import "HQViewController.h"
#import "HQNavigationController.h"
#import "HQDetailViewController.h"
#import "UIButton+Extension.h"


@interface HQBuyListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *buyList;

@end

@implementation HQBuyListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.title = @"购物清单";

    
    
    //添加左边按钮
    UIButton *leftButton = [UIButton buttonWithImage:@"nav_button_back" title:@"返回" font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [leftButton addTarget:self action:@selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

    //添加右边导航按钮
    UIButton *rightButton = [UIButton buttonWithImage:@"nav_button" title:@"购买" font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [rightButton addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
}

- (void)loadView{
    self.tableView = [[UITableView alloc] init];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.view = self.tableView;
}

//数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.buyList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    HQZhou *zhou = self.buyList[indexPath.row];
    cell.textLabel.text = zhou.name;
    cell.textLabel.textColor = [UIColor orangeColor];
    cell.detailTextLabel.text = zhou.flavor;
    cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    
    
    
    return cell;
}

//点击cell的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HQDetailViewController *detailVC = [[HQDetailViewController alloc] init];
    HQZhou *zhou = self.buyList[indexPath.item];
    detailVC.zhou = zhou;
    HQNavigationController *nav = [[HQNavigationController alloc] initWithRootViewController:detailVC];
    
    [self presentViewController:nav animated:true completion:nil];
}


//删除清单选项
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.buyList removeObjectAtIndex:indexPath.row];
        [self removeBuyList:indexPath.row];
        [self.tableView reloadData];
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

//删除相应的归档数据
- (BOOL)removeBuyList:(NSInteger)index{
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *filePath = [docPath stringByAppendingPathComponent:@"buy.archieve"];
    
    NSMutableArray *templeArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    [templeArr removeObjectAtIndex:index];
    
    [NSKeyedArchiver archiveRootObject:templeArr toFile:filePath];
    
    
    return YES;
}



//懒加载
- (NSMutableArray *)buyList{
    if (_buyList == nil) {
        
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *filePath = [docPath stringByAppendingPathComponent:@"buy.archieve"];
        
        _buyList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    }
    return _buyList;
}

//购买
- (void)buy
{

    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定购买");
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    UIAlertController *alertControllerNo = [UIAlertController alertControllerWithTitle:@"提示" message:@"你的购物清单里还空空如也,请到主面选购喜欢的粥品" preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"购买" message:@"您将以18块的购买清单所列粥品" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *goToMainPage = [UIAlertAction actionWithTitle:@"主页" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        HQViewController *viewController = [sb instantiateViewControllerWithIdentifier:@"main"];
        [self.navigationController pushViewController:viewController animated:YES];
    }];
    
    [alertControllerNo addAction:cancelAction];
    [alertControllerNo addAction:goToMainPage];
    
    [alertController addAction:cancelAction];
    [alertController addAction:sureAction];
    

    
    if (self.buyList.count == 0) {
        [self presentViewController:alertControllerNo animated:YES completion:nil];
    }
    else {
            [self presentViewController:alertController animated:YES completion:nil];
    }
    

}



@end
